package com.eightbears.beboy.ui.user.activity;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.MyProfitAdapter;
import com.eightbears.beboy.adapter.ProfitWarterAdapter;
import com.eightbears.beboy.adapter.ShopInfoAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ProfitEntity;
import com.eightbears.beboy.entity.ProfitWaterEntity;
import com.eightbears.beboy.entity.ShopListEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.PARAMS_PAGE_SIZE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_MLM_PROFIT;
import static com.eightbears.beboy.base.util.CommonAPI.URL_MLM_PROFIT_WARTER;
import static com.eightbears.beboy.base.util.CommonAPI.delayMillis;
import static com.eightbears.beboy.ui.user.fragment.FindFragment.PARAMS_SHOP_ID;

/**
 * 利润流水
 */
public class MyProfitActivity extends BaseActivity implements
        BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.view_empty)
    View view_empty;

    private ProfitEntity mProfitEntity;
    private int page = 1;
    private boolean isErr = true;
    private int isEnd = 0;

    private MyProfitAdapter mMyProfitAdapter;
    private LinearLayout mLl_all_profit;
    private ProfitWarterAdapter mProfitWarterAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_profit);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText(R.string.text_profit_warter);
        initAdapter();
    }

    @Override
    protected void initData() {
        getDate();
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    private void initAdapter() {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
        swipeLayout.setColorSchemeColors(getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color));
        rv_list.setLayoutManager(new MyLinearLayoutManager(mContext));
        mProfitWarterAdapter = new ProfitWarterAdapter();
        mProfitWarterAdapter.openLoadAnimation();
        mProfitWarterAdapter.setOnLoadMoreListener(this);
        rv_list.setAdapter(mProfitWarterAdapter);
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
        mProfitWarterAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                listBean = (ShopListEntity.DataBean.ListBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.shop_info_root:
//                        startLocalActivity((Activity) mContext, ShopDetailActivity.class, PARAMS_SHOP_ID, listBean.getShop_id());
                        break;
                }
            }
        });
    }

    private ProfitWaterEntity mProfitWaterEntity;

    private void getDate() {
        OkHttpUtils.get().url(URL_MLM_PROFIT_WARTER)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("page", String.valueOf(page))
                .addParams("size", PARAMS_PAGE_SIZE)
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                swipeLayout.setRefreshing(false);
                mProfitWaterEntity = GsonUtil.fromJson(response, ProfitWaterEntity.class);
                if (CONNECTION_STATUS_SUCCESS.equals(String.valueOf(mProfitWaterEntity.getStatus_code()))) {
                    isEnd = mProfitWaterEntity.getData().getIs_end();
                    if (page == 1) {
                        if (mProfitWaterEntity.getData().getList().size() == 0) {
                            view_empty.setVisibility(View.VISIBLE);
                        } else {
                            view_empty.setVisibility(View.GONE);
                            mProfitWarterAdapter.setNewData(mProfitWaterEntity.getData().getList());
                        }
                    } else {
                        mProfitWarterAdapter.addData(mProfitWaterEntity.getData().getList());
                        mProfitWarterAdapter.loadMoreComplete();
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                swipeLayout.setRefreshing(false);
                isErr = false;
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError((Activity) mContext, status_error);
            }
        });
    }

    @Override
    public void onRefresh() {
        mProfitWarterAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isErr = true;
                page = 1;
                mProfitWarterAdapter.getData().clear();
                getDate();
                mProfitWarterAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isEnd == 1) {
                    mProfitWarterAdapter.loadMoreEnd(true);
                } else {
                    if (isErr) {
                        page++;
                        getDate();
                    } else {
                        isErr = true;
                        Toast.makeText(mContext, R.string.network_anomalies, Toast.LENGTH_LONG).show();
                        mProfitWarterAdapter.loadMoreFail();
                    }
                }
            }
        }, delayMillis);
    }
}
